package DispatchSys;

import java.util.Date;
import Shared.Form;

/**
 * The FormRecorder module, see Architecture report for specifications.
 * 
 */
public class FormRecorder {

	// Database
	private Database db;

	// Module of duplicate detection
	private DuplicateDetection duplicate;

	/**
	 * Creates a FormRecorder object.
	 */
	public FormRecorder() {
		this.db = Database.getInstance();
		this.duplicate = new DuplicateDetection();
	}

	/**
	 * Add the form "form" as an emergency into the Database if it is not
	 * already represented.
	 * 
	 * @param form
	 * @return true if it was added, false otherwise.
	 */
	public boolean enqueueForm(Form form) {

		ERInfo info = this.translateToERInfo(form);
		ERInfo duplicateERInfo;

		if ((duplicateERInfo = this.duplicate.checkDuplicate(info)) != null) {
			duplicateERInfo.addForm(form);
			return false;
		} else {
			info.addForm(form);
			db.addER(info);
			return true;

		}
	}

	/**
	 * Translates the form to an emergency that will be registered in the DataBase.
	 * 
	 * @param form
	 *            - the form to be translated
	 * @return the ERInfo resulting from the translation
	 */
	private ERInfo translateToERInfo(Form form) {
		return new ERInfo(form.getCalleeName(), form.getCalleeNumber(), form.getCalleePosition(), form.isUrgent(),
				new Date(), form.getLocation(), form.getDescription());
	}

}
